//
//  HttpHelper.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/14.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "HttpHelper.h"
#import <AFNetworking.h>
#import "UserModel.h"
#import "PINCache.h"
@implementation HttpHelper

static AFHTTPSessionManager * instance;
NSString * ServerUrl=@"http://39.104.87.146";
+ (AFHTTPSessionManager  *)shareInstance
{
    
        static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       
         instance = [AFHTTPSessionManager manager];
        
        [ instance.requestSerializer setValue:@"ios" forHTTPHeaderField:@"system-type"];
        
        [ instance.requestSerializer setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forHTTPHeaderField:@"system-version"];
        
        UserModel *userModel = [[PINCache sharedCache] objectForKey:@"user"];
        NSString *token =@"";
        NSLog(@"token=%@",userModel.token);
        if(userModel !=nil){
            token=userModel.token;
        }
        [instance.requestSerializer setValue:token forHTTPHeaderField:@"token"];
        
        });
    return instance;
}


+ (void)request:(NSString *)path :(NSString *)method :(NSMutableDictionary *)params successBlock:(void (^)(NSDictionary *responseDic))success failureBlock:(void (^)(NSError *error))failure{
    
    if  ([method  isEqual:@"GET"]) {
        
        [[self shareInstance] GET: [NSString stringWithFormat:@"%@/%@", ServerUrl, path] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            success(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
        
    } else {
        
        [[self shareInstance] POST: [NSString stringWithFormat:@"%@/%@", ServerUrl, path] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            success(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
   }
    

}


+ (instancetype) sharedInstance {
    
    static id sharedInstance = nil;
    
    static dispatch_once_t oneToken;
    dispatch_once (&oneToken,^{
        
        sharedInstance = [[self alloc]init];
        
    });
    return sharedInstance;
}




@end
